package com.example.fang.demodruid.cocurrency6;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.IntStream;

public class MyTest2 {

    private ReadWriteLock readWriteLock=new ReentrantReadWriteLock();

    public static void main(String[] args){
        MyTest2 myTest2=new MyTest2();
        IntStream.range(0,10).forEach(i->{
            new Thread(()->{
                myTest2.method();
            }).start();
        });

    }

    public void method(){
        try {
            //readWriteLock.readLock().lock();
            readWriteLock.writeLock().lock();
            try {
              Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("method");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
           // readWriteLock.readLock().unlock();
            readWriteLock.writeLock().unlock();
        }

    }
}
